/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.persistence.entity.report;

import org.apache.commons.lang.StringUtils;

import javax.persistence.*;
import java.util.Collection;

/**
 * Entity class for report designs packages.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 23:11 21.06.11
 *
 * @author Burt Parkers, Peter Herber
 * @version 1.0
 */
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"name", "version"}))
@NamedQueries({
    @NamedQuery(name = "ReportDesignPackage.findByNameAndVersion",
                query = "select reportDesignPackage from ReportDesignPackage reportDesignPackage where reportDesignPackage.name=?1 and "
                        + "reportDesignPackage.version=?2"),
    @NamedQuery(name = "ReportDesignPackage.findAllSorted",
                query = "select reportDesignPackage from ReportDesignPackage reportDesignPackage "+
                        "order by reportDesignPackage.name, reportDesignPackage.version")
})
public class ReportDesignPackage {

    /** The id field. */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    /** Render engine type */
    @Column(nullable = false)
    private RenderingEngineEnum renderingEngine;

    /** Name of the design Package. */
    @Column(nullable = false, length = 50)
    private String name;

    /** Name of the design */
    @Column(nullable = false, length = 10)
    private String version;

    @OneToMany(mappedBy = "reportDesignPackage", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
    private Collection<ReportDesign> reportDesigns;

    /**
     * Returns the id.
     *
     * @return the id.
     */
    public int getId() {
        return id;
    }

    /**
     * Sets the id.
     *
     * @param id the id.
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * Sets the version.
     *
     * @param version The version value to set.
     */
    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * Gets the version.
     *
     * @return The version value.
     */
    public String getVersion() {
        return version;
    }

    /**
     * Gets the renderingEngine.
     *
     * @return The renderingEngine value.
     */
    public RenderingEngineEnum getRenderingEngine() {
        return renderingEngine;
    }

    /**
     * Sets the renderingEngine.
     *
     * @param renderingEngine The renderingEngine value to set.
     */
    public void setRenderingEngine(RenderingEngineEnum renderingEngine) {
        this.renderingEngine = renderingEngine;
    }

    /**
     * Gets the reportDesigns.
     *
     * @return The reportDesigns value.
     */
    public Collection<ReportDesign> getReportDesigns() {
        return reportDesigns;
    }

    /**
     * Sets the reportDesigns.
     *
     * @param reportDesigns The reportDesigns value to set.
     */
    public void setReportDesigns(Collection<ReportDesign> reportDesigns) {
        this.reportDesigns = reportDesigns;
    }

    /**
     * Gets the name.
     *
     * @return The name value.
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name.
     *
     * @param name The name value to set.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Finds a report design by it's name
     *
     * @param name the report design name
     * @return the report design.
     */
    public ReportDesign getReportDesignByName(String name) {
        for (ReportDesign reportDesign : reportDesigns) {
            if (StringUtils.equals(reportDesign.getName(), name)) {
                return reportDesign;
            }
        }

        return null;
    }

    @Override
    public String toString() {
        return "ReportDesignPackage{" + "id=" + id + ", renderingEngine=" + renderingEngine + ", name='" + name + '\''
               + ", version='" + version + '\'' + ", reportDesigns=" + reportDesigns + '}';
    }


}
